# include <err.h>
# include <fcntl .h>
# include <libelf .h>

# include <stdio .h>
# include <stdlib .h>
# include <sysexits .h>
# include <unistd .h>

int main ( int argc , char ** argv )
{
	int fd;
	Elf *e;

	char *k;
	Elf_Kind ek;

	if ( argc != 2)
		errx ( EX_USAGE , " usage : %s file - name ", argv [0]);
	if ( elf_version ( EV_CURRENT ) == EV_NONE )

		errx ( EX_SOFTWARE , " ELF  library   initialization  "" failed : %s", elf_errmsg ( -1));
	if (( fd = open ( argv [1] , O_RDONLY , 0)) < 0)
		err ( EX_NOINPUT , " open  \%s\"  failed ", argv [1]);
	if ((e = elf_begin (fd , ELF_C_READ, NULL )) == NULL )
		errx ( EX_SOFTWARE , " elf_begin ()  failed : %s.",
				elf_errmsg ( -1));

	ek = elf_kind (e);

	switch (ek)
	{
		case ELF_K_AR :
				k = "ar (1)  archive ";
				break ;
		case ELF_K_ELF :
				k = "elf  object ";
				break ;
		case ELF_K_NONE :
				k = " data ";
				break ;
		default :
				k = " unrecognized ";
	}
( void ) printf ("%s: %s\n", argv [1] , k);
( void ) elf_end (e);

( void ) close (fd );
exit ( EX_OK );
return 0;
}


/*
 * % cc -o prog1 prog1 .c -lelf
% ./ prog1 prog1
prog1 : elf object
% ./ prog1 /usr/lib/ libc .a
/usr/lib/ libc .a: ar (1) archive
 */

/*
 *
 * e_ident[] offsets 0x7f EI_mago
 * 				'E' EI_magi
 * 				'L' EI_Mag2
 * 				'F' Ei_Mag3
 * 				class: 32 or 64 bit EI_class
 * 				endianess EI_Data
 * 				Elf version	EI_version
 * 				desired ABI EI_OSABI
 * 				EI_abiversion
 */



/*USING 32 bit header
 * typedef struct { typedef struct {
1 unsigned char e_ident[16]; unsigned char e_ident[16];
2 uint16_t e_type; uint16_t e_type;
3 uint16_t e_machine; uint16_t e_machine;
uint32_t e_version; uint32_t e_version;
uint32_t e_entry; uint32_t e_entry;
4 uint32_t e_phoff; uint64_t e_phoff;
5 uint32_t e_shoff; uint64_t e_shoff;
uint32_t e_flags; uint32_t e_flags;
uint16_t e_ehsize; uint16_t e_ehsize;
uint16_t e_phentsize; uint16_t e_phentsize;
6 uint16_t e_phnum; uint16_t e_phnum;
7 uint16_t e_shnum; uint16_t e_shnum;
8 uint16_t e_shstrndx; uint16_t e_shstrndx;
} Elf32_Ehdr; } Elf64_Ehdr;
 *
 */
